﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace Sern.Common
{
    public static class ClientWrapperFactory
    {
        public class Client<T> : IClient<T>
        {
            private T _channel;

            internal Client(T channel)
            {
                _channel = channel;
            } 

            public T Proxy
            {
                get { return _channel; }
            }

            public void Close()
            {
                var _ = (_channel as IClientChannel);
                if (_ != null)
                {
                    _.Close();
                    _.Dispose();
                }
            }

            public void Dispose()
            {
                Close();
            }
        }

        /// <summary>
        /// Wrap the client proxy in a disposable interface.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="proxy"></param>
        /// <returns></returns>
        public static IClient<T> Wrap<T>(this T proxy)
        {
            return new Client<T>(proxy);
        }
    }
}
